#!/bin/ksh

#
# The point of the script is to simply prevent the case where
# the clock on the HMC may be initially set into the future,
# and this could jepordize the backup of critical HMC data. 
#
# When the user's sets the date/time via either HMC GUI, this
# routine will be called. It will be valid for one time only
# since the comparison file created by the 'finalizeImage'
# script will be erased at the end of this processing.
#

#
# Just in case we have NLS troubles reading system information...
# ...and sure enough we do without this setting! awk(?) command
# returning parsed data inconsistenly
#
LANG=en_US
export LANG

DAT_FILE=/opt/hsc/com/ibm/hsc/websm/launch/hscmgt/hscbuild.dat
DAT_FILE2=/opt/hsc/com/ibm/hsc/websm/launch/hscmgt/rpminst.dat
CURRENT_TIME=/tmp/cur_time
CUR_RPM_PACKAGE_DATE=/tmp/cur_rpm_date


if [ -f /opt/hsc/data/.time_not_set ]; then
    #
    # Set global RPM package install date stamp to last installed RPM package
    # "Install date:" value
    #
    if [ ! -f $DAT_FILE2 ]; then
        rpm -qa > /tmp/rpm.list
        chmod 777 /tmp/rpm.list

        for i in `cat /tmp/rpm.list`
        do
           x=`rpm -qi $i | grep "Install date:" | awk '{print $4" "$5" "$6" "$7" "$8" "$9}'`
   
           touch $CUR_RPM_PACKAGE_DATE --date="$x"
   
           if [ -f $DAT_FILE2 ]; then
               if [ $CUR_RPM_PACKAGE_DATE -nt $DAT_FILE2 ]; then
                   # copy and preserve the date!
                   cp -p $CUR_RPM_PACKAGE_DATE $DAT_FILE2
               fi
           else
               # copy and preserve the date!
               cp -p $CUR_RPM_PACKAGE_DATE $DAT_FILE2
           fi
        done
        
        # clean up
        rm -f $CUR_RPM_PACKAGE_DATE
        rm -f /tmp/rpm.list
    fi
    
    
    #
    # Now ensure the file marker is not set in the future
    #
    touch $CURRENT_TIME
    if [ $DAT_FILE -nt $CURRENT_TIME ]; then
        touch $DAT_FILE
    fi
    
    # clean up
    rm -f /opt/hsc/data/.time_not_set
    rm -f $CURRENT_TIME
fi
exit 0
